<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>教学任务管理</title>
</head>
<body>
<div id="app">
    <!--搜索框-->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="班级名：">
            <el-input v-model="searchForm.cclasses.classesName" placeholder="模糊查询"></el-input>
        </el-form-item>
        <el-form-item label="课程名：">
            <el-input v-model="searchForm.cclasses.course.courseName" placeholder="模糊查询"></el-input>
        </el-form-item>
        <el-form-item label="教师名：">
            <el-input v-model="searchForm.teacher.teacherName" placeholder="模糊查询"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="selectByCondition">查询</el-button>
        </el-form-item>
    </el-form>

    <!-- 批量    -->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="addFormVisible = true">添加教学任务</el-button>
    </el-row>

    <!--    表格-->
    <el-table
            :data="tableData"
            style="width: 100%"
            @selection-change="handleSelectionChange"
    >
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="ID"
                align="center">
        </el-table-column>
        <el-table-column
                prop="cclasses.classesName"
                label="班级名"
                align="center">
        </el-table-column>
        <el-table-column
                prop="cclasses.course.courseName"
                label="课程名"
                align="center">
        </el-table-column>
        <el-table-column
                prop="teacher.teacherName"
                label="教师名"
                align="center">
        </el-table-column>
        <el-table-column
                prop="weekNode"
                label="周节"
                align="center">
        </el-table-column>
        <el-table-column
                prop="startWeek"
                label="开始周"
                align="center">
        </el-table-column>
        <el-table-column
                prop="endWeek"
                label="结束周"
                align="center">
        </el-table-column>

        <el-table-column label="操作" align="center">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除
                </el-button>
            </template>
        </el-table-column>

    </el-table>

    <!--新增 对话框   -->
    <el-dialog title="添加教学任务" :visible.sync="addFormVisible" width="70%">

        <!--搜索框-->
        <el-form :inline="true" :model="searchForm2" class="demo-form-inline">
            <el-form-item label="班级名：">
                <el-input v-model="searchForm2.classesName" placeholder="模糊查询"></el-input>
            </el-form-item>
            <el-form-item label="课程名：">
                <el-input v-model="searchForm2.course.courseName" placeholder="模糊查询"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="selectByCondition2">查询</el-button>
            </el-form-item>
        </el-form>
        <!--    表格-->
        <el-table
                :data="tableData2"
                style="width: 100%"
        >
            <el-table-column
                    type="index"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="classesName"
                    label="班级名"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="course.courseName"
                    label="课程名"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="remark"
                    label="备注"
                    align="center">
            </el-table-column>

            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="primary"
                            @click="handleEdit2(scope.$index, scope.row)">选中
                    </el-button>
                </template>
            </el-table-column>

        </el-table>

        <!--  分页  -->
        <div class="block">
            <el-pagination
                    @size-change="handleSizeChange2"
                    @current-change="handleCurrentChange2"
                    :current-page="currentPage2"
                    :page-sizes="[3, 6, 9]"
                    :page-size="pageSize2"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalCount2">
            </el-pagination>
        </div>

        <!--选中结果 -->
        <el-form :inline="true" :model="taskForm" class="demo-form-inline" style="margin-top: 20px">
            <el-form-item label="班级名">
                <el-input v-model="taskForm.cclasses.classesName" :disabled="true" style="width:370px"></el-input>
            </el-form-item>

            <el-form-item label="课程名">
                <el-input v-model="taskForm.cclasses.course.courseName" :disabled="true" ></el-input>
            </el-form-item>

            <el-form-item label="教师名">
                <el-select v-model="value"  style="width:100px">
                    <el-option
                            clearable
                            v-for="item in options"
                            :key="item.id"
                            :label="item.teacherName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="周节">
                <el-input v-model="taskForm.weekNode" style="width:100px"></el-input>
            </el-form-item>
            <el-form-item label="开始周">
                <el-input v-model="taskForm.startWeek" style="width:100px"></el-input>
            </el-form-item>
            <el-form-item label="结束周">
                <el-input v-model="taskForm.endWeek" style="width:100px"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addTask">提交</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>

    <!--编辑 对话框   -->
    <el-dialog title="编辑教学任务" :visible.sync="updateFormVisible" width="50%">
        <!--选中结果 -->
        <el-form :inline="true" :model="taskForm" class="demo-form-inline" style="margin-top: 20px">
            <el-form-item label="班级名">
                <el-input v-model="taskForm.cclasses.classesName" :disabled="true" style="width:370px"></el-input>
            </el-form-item>

            <el-form-item label="课程名">
                <el-input v-model="taskForm.cclasses.course.courseName" :disabled="true" ></el-input>
            </el-form-item>

            <el-form-item label="教师名">
                <el-select v-model="value" :placeholder="taskForm.teacher.teacherName" style="width:100px">
                    <el-option
                            clearable
                            v-for="item in options"
                            :key="item.id"
                            :label="item.teacherName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <br>
            <el-form-item label="周节">
                <el-input v-model="taskForm.weekNode" style="width:100px"></el-input>
            </el-form-item>
            <el-form-item label="开始周">
                <el-input v-model="taskForm.startWeek" style="width:100px"></el-input>
            </el-form-item>
            <el-form-item label="结束周">
                <el-input v-model="taskForm.endWeek" style="width:100px"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="updateTask">提交</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>


    <!--  分页  -->
    <div class="block">
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalCount">
        </el-pagination>
    </div>

</div>


<!--导入资源-->
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script src="js/axios-0.18.0.js"></script>


<script>
    //创建vue核心对象
    new Vue({
        el: "#app",
        mounted() {
            this.selectByCondition();
            //查课程班级
            this.selectByCondition2();
        },
        methods: {

            //编辑  删除
            handleEdit(index, row) {
                //数据填入  利用JSON解除模型绑定
                this.taskForm = JSON.parse(JSON.stringify(row));
                this.selectTeacher();
                //打开编辑表单
                this.updateFormVisible = true;
            },

            handleDelete(index, row) {
                this.multipleSelection[0] = row;
                this.deleteByIds();
            },

            //查询
            selectByCondition() {
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/task/selectByCondition?"+
                        "currentPage=" +_this.currentPage+"&pageSize="+_this.pageSize,
                    data: this.searchForm
                }).then(function (resp) {
                    //设置表格数据
                    _this.tableData = resp.data.list;
                    _this.totalCount = resp.data.total;
                })
            },

            //查课程绑定的所有教师
            selectTeacher(){
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/cteacher/selectTeacher",
                    data: this.taskForm.cclasses.course
                }).then(function (resp) {
                    //设置表格数据
                    _this.options = resp.data;
                    _this.value = _this.taskForm.teacher.id;
                })
            },



            //分页
            handleSizeChange(val) {
                //重新设置每页条目数
                this.pageSize = val;
                //重新查询
                this.selectByCondition();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                //重新查询
                this.selectByCondition();
            },


            //复选事件
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            //批量禁用
            deleteByIds() {
                var _this = this;
                //未选中
                if (this.multipleSelection.length == 0){
                    //弹出消息提示
                    _this.$message({
                        message: '无选中数据项！',
                        type: 'error',
                        center: true
                    });
                }else {
                    this.$confirm('此操作将删除该数据项，是否继续？', "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }).then(() => {
                        //用户点击确认按钮
                        for (let i = 0; i < this.multipleSelection.length; i++) {
                            this.selectIds[i] = this.multipleSelection[i].id;
                        }
                        //发送ajax请求
                        axios({
                            method: "post",
                            url: 'http://localhost:8080/task/deleteByIds',
                            data: _this.selectIds
                        }).then(function (resp) {
                            if (resp.data == "success") {
                                //删除成功重新查询
                                _this.selectByCondition();
                                //弹出消息提示
                                _this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    center: true
                                });
                            }

                        })
                    })
                }

            },


            //提交添加表单
            addTask() {
                var _this = this;
                if (this.value.length!=0){
                    this.taskForm.teacher.id = this.value;
                }
                axios({
                    method: "post",
                    url: "http://localhost:8080/task/add",
                    data: _this.taskForm
                }).then(function (resp) {
                    if (resp.data == "success") {
                        //关闭弹窗
                        _this.addFormVisible = false;
                        //重新查询数据
                        _this.selectByCondition();
                        //弹出提示信息
                        _this.$message({
                            message: '修改成功',
                            type: 'success',
                            center: true
                        });
                    }else {
                        //关闭弹窗
                        _this.addFormVisible = false;
                        //弹出提示信息
                        _this.$message({
                            message: '教学任务已存在！',
                            type: 'error',
                            center: true
                        });
                    }
                })
            },

            //提交修改表单
            updateTask() {
                var _this = this;
                if (this.value.length!=0){
                    this.taskForm.teacher.id = this.value;
                }
                axios({
                    method: "post",
                    url: "http://localhost:8080/task/update",
                    data: _this.taskForm
                }).then(function (resp) {
                    if (resp.data == "success") {
                        //关闭弹窗
                        _this.updateFormVisible = false;
                        //重新查询数据
                        _this.selectByCondition();
                        //弹出提示信息
                        _this.$message({
                            message: '修改成功',
                            type: 'success',
                            center: true
                        });
                    }else {
                        //关闭弹窗
                        _this.updateFormVisible = false;
                        //弹出提示信息
                        _this.$message({
                            message: '教学任务已存在！',
                            type: 'error',
                            center: true
                        });
                    }
                })
            },


            //选中编辑
            handleEdit2(index, row) {
                //数据填入  利用JSON解除模型绑定
                this.taskForm.cclasses = JSON.parse(JSON.stringify(row));
                this.selectTeacher();
            },
            //课程查询
            selectByCondition2() {
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/cclasses/selectByCondition?" +
                        "currentPage=" + _this.currentPage2 + "&pageSize=" + _this.pageSize2,
                    data: this.searchForm2
                }).then(function (resp) {
                    //设置表格数据
                    _this.tableData2 = resp.data.list;
                    _this.totalCount2 = resp.data.total;
                })
            },
            //课程分页
            handleSizeChange2(val) {
                //重新设置每页条目数
                this.pageSize2 = val;
                //重新查询
                this.selectByCondition2();
            },
            handleCurrentChange2(val) {
                this.currentPage2 = val;
                //重新查询
                this.selectByCondition2();
            },


        },


        //数据
        data() {
            return {
                //分页
                //当前页
                currentPage: 1,
                //总页数
                totalCount: 0,
                //当前条目数
                pageSize: 5,


                //选着的数组
                selectIds: [],

                //添加可见
                addFormVisible: false,
                //修改可见
                updateFormVisible: false,
                searchForm: {
                    cclasses:{
                        course:{}
                    },
                    teacher:{}
                },

                oldName:'',
                //添加表单
                taskForm: {
                    cclasses:{
                        classesName: '',
                        course:{
                            courseName:''
                        }
                    },
                    teacher:{
                        id:'',
                        teacherName:'',
                    },
                    weekNode:'',
                    startWeek:"",
                    endWeek:"",
                },

                //复选的值？
                multipleSelection: [],

                //表格数据
                tableData: [],


                //分配课程
                //当前页
                currentPage2: 1,
                //总页数
                totalCount2: 0,
                //当前条目数
                pageSize2: 3,
                //搜索表单
                searchForm2: {
                    classesName:'',
                    course:{
                        courseName:''
                    }
                },
                tableData2: [],

                options: [],
                value:''

            }
        }
    })

</script>


</body>
</html>